package com.sword.controller.stock;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.stock.EnterstockDetail;
import com.sword.pojo.stock.Leavestock;
import com.sword.pojo.stock.LeavestockDetail;
import com.sword.service.impl.stock.LeaveStockDetailServiceImpl;
import com.sword.service.impl.stock.LeaveStockServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/stock")
public class LeaveStockController {
    @Autowired
    private LeaveStockServiceImpl leaveStockService;
    @Autowired
    private LeaveStockDetailServiceImpl leaveStockDetailService;

    @RequestMapping("/leavestock")
    public String leavestock() {
        return "views/stock/leaveStock";
    }
    @RequestMapping("/leavestockdetail")
    public String leavestockdetail() {
        return "views/stock/leaveStockDetail";
    }

    /**
   查询所有出库单
    */
    @RequestMapping("/getLeavestocks")
    @ResponseBody
    public PageResult getLeavestocks(@RequestBody QueryPageBean queryPageBean) {
        try {
            return leaveStockService.queryLeaveStock(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_LEAVESTOCK_FAIL, null, null);
        }
    }
    /**
     * 添加出库单功能
     */

    @ResponseBody
    @RequestMapping("/addLeavestock")
    public Result addLeavestock(@RequestBody Leavestock leaveStock) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            leaveStock.setEmployeeid(userId);
            // 判断是否添加成功
            Integer flag = leaveStockService.addLeaveStock(leaveStock);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_LEAVESTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_LEAVESTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_LEAVESTOCK_SUCCESS, null);
    }
    /**
     * 修改出库单
     */
    @ResponseBody
    @RequestMapping("editLeavestock")
    public Result editLeavestock(@RequestBody Leavestock leaveStock) {
        try {
            Integer flag = leaveStockService.editLeaveStock(leaveStock);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_LEAVESTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_LEAVESTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_LEAVESTOCK_SUCCESS, null);
    }
    /**
     * 审核入库明细
     */
    @ResponseBody
    @RequestMapping("examineLeavestockdetail")
    public Result examineLeavestockdetail(@RequestBody LeavestockDetail leavestockDetail) {
        try {
            Integer flag = leaveStockDetailService.examineLeaveStockDetail(leavestockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_ENTERSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 弃审入库明细
     */
    @ResponseBody
    @RequestMapping("unexamineLeavestockdetail")
    public Result unexamineLeavestockdetail(@RequestBody LeavestockDetail leavestockDetail) {
        try {
            Integer flag = leaveStockDetailService.unexamineLeaveStockDetail(leavestockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_ENTERSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 删除出库单
     */

    @ResponseBody
    @RequestMapping("deleteLeavestock")
    public Result deleteLeavestock(Integer id) {
        try {
            Integer flag = leaveStockService.deleteLeaveStock(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_LEAVESTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_LEAVESTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_LEAVESTOCK_SUCCESS, null);
    }

    /**
     查询所有出库明细
     */
    @RequestMapping("/getLeavestockdetails")
    @ResponseBody
    public PageResult getLeavestockdetails(@RequestBody QueryPageBean queryPageBean) {
        try {
            return leaveStockDetailService.queryLeaveStockDetail(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_LEAVESTOCKDETAIL_FAIL, null, null);
        }
    }

    /**
     * 添加出库明细
     */

    @ResponseBody
    @RequestMapping("/addLeavestockdetail")
    public Result addLeavestockdetail(@RequestBody LeavestockDetail leaveStockDetail) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            leaveStockDetail.setCreateid(userId);
            // 判断是否添加成功
            Integer flag = leaveStockDetailService.addLeaveStockDetail(leaveStockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_LEAVESTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_LEAVESTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_LEAVESTOCK_SUCCESS, null);
    }
    /**
     * 修改出库明细
     */
    @ResponseBody
    @RequestMapping("editLeavestockdetail")
    public Result editLeavestockdetail(@RequestBody LeavestockDetail leaveStockDetail) {
        try {
            Integer flag = leaveStockDetailService.editLeaveStockDetail(leaveStockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_LEAVESTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_LEAVESTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_LEAVESTOCKDETAIL_SUCCESS, null);
    }

    /**
     * 删除出库明细
     */

    @ResponseBody
    @RequestMapping("deleteLeavestockdetail")
    public Result deleteLeavestockdetail(Integer id) {
        try {
            Integer flag = leaveStockDetailService.deleteLeaveStockDetail(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_LEAVESTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_LEAVESTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_LEAVESTOCKDETAIL_SUCCESS, null);
    }
}
